package io.renren.modules.sys.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysConfigEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.sys.entity.MerchantEntity;
import io.renren.modules.sys.service.MerchantService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 代理商商户
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2019-10-11 10:46:27
 */
@RestController
@RequestMapping("sys/merchant")
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    @Value("${webDownLoad.upload-path}")
    private  String path;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:merchant:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = merchantService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 商户列表
     */
    @RequestMapping("/merchantlist")
    @RequiresPermissions("sys:merchant:merchantlist")
    public R merchantlist(@RequestParam Map<String, Object> params){
        PageUtils page = merchantService.merchantlist(params);

        return R.ok().put("page", page);
    }

    /**
     * 商户列表
     */
    @RequestMapping("/merchantpage")
    @RequiresPermissions("sys:merchant:merchantpage")
    public R merchantpage(@RequestParam Map<String, Object> params){
        PageUtils page = merchantService.merchantPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 总商户数
     */
    @RequestMapping("getMerchantCounts")
    public R getMoneyTotal(){
        List<MerchantEntity> list = merchantService.list();
        Integer merchantCounts = 0;
        for (MerchantEntity entity : list) {
            merchantCounts+=Integer.valueOf(entity.getId());
        }
        return R.ok().put("merchantCounts", merchantCounts);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:merchant:info")
    public R info(@PathVariable("id") Integer id){
        MerchantEntity merchant = merchantService.getById(id);

        return R.ok().put("merchant", merchant);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoByUserid/{id}")
    @RequiresPermissions("sys:merchant:infoByUserid")
    public R infoByUserid(@PathVariable("id") Integer id){
        MerchantEntity merchant = merchantService.infoByUserid(id);

        return R.ok().put("merchant", merchant);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:merchant:save")
    public R save(@RequestBody MerchantEntity merchant){
        merchantService.save(merchant);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:merchant:update")
    public R update(@RequestBody MerchantEntity merchant){
        ValidatorUtils.validateEntity(merchant);
        merchantService.updateById(merchant);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:merchant:delete")
    public R delete(@RequestBody Integer[] ids){
        merchantService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteById")
    public R deleteById(@RequestBody Integer id){
        merchantService.removeById(id);

        return R.ok();
    }

    /**
     * 获取代理商密码线条数
     */
    @RequestMapping("/queryLineCodeNum/{id}")
    public R queryLineCodeNum(@PathVariable("id") Integer id){
        int num = merchantService.queryLineCodeNum(id);

        return R.ok().put("num", num);
    }

//    private final String  urlLogo="http://127.0.0.1:8080/gongxiangchongdian-admin/";
//    private final String  urlLogo="https://dyc.ctolab.cn/gongxiangchongdian-admin/";
    private final String  urlLogo="/data/uploadImg/";
    @RequestMapping(value="/uploadLogo",method= RequestMethod.POST)
    @ResponseBody
    public R uploadLogo(MultipartFile file, HttpServletRequest request)throws Exception{
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String storePath= path;//存放我们上传的文件路径
        String urlPath=urlLogo;
        if (!file.isEmpty()) {
            String fileName =new Date().getTime()+".jpg";
            File filepath = new File(storePath, fileName);
//            if (!filepath.getParentFile().exists()) {
//                filepath.getParentFile().mkdirs();//如果目录不存在，创建目录
//            }
            try {
                file.transferTo(new File(storePath+File.separator+fileName));//把文件写入目标文件地址
            } catch (Exception e) {
                e.printStackTrace();
                return R.error();
            }
            Map<String, String> content=new HashMap<>();
            content.put("first","您好,图片已经更新");
            content.put("keyword1","图片更新");
            content.put("keyword3", sdf.format(new Date()));
            return R.ok().put("fileName",urlPath+fileName).put("url",urlPath+fileName);
        }else {
            return R.error();
        }
    }

//    private final String  url="http://127.0.0.1:8080/gongxiangchongdian-admin";
    private final String  url="https://dyc.ctolab.cn/gongxiangchongdian-admin/";
    @RequestMapping(value="/uploadImg",method= RequestMethod.POST)
    @ResponseBody
    public R upload(MultipartFile file, HttpServletRequest request)throws Exception{
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String storePath= path;//存放我们上传的文件路径
        String urlPath=url;
        if (!file.isEmpty()) {
            String fileName =new Date().getTime()+".jpg";
            File filepath = new File(storePath, fileName);
            if (!filepath.getParentFile().exists()) {
                filepath.getParentFile().mkdirs();//如果目录不存在，创建目录
            }
            try {
                file.transferTo(new File(storePath+File.separator+fileName));//把文件写入目标文件地址
            } catch (Exception e) {
                e.printStackTrace();
                return R.error();
            }
            Map<String, String> content=new HashMap<>();
            content.put("first","您好,图片已经更新");
            content.put("keyword1","图片更新");
            content.put("keyword3", sdf.format(new Date()));
            return R.ok().put("fileName",urlPath+fileName).put("url",urlPath+fileName);
        }else {
            return R.error();
        }
    }
}
